import { Logger } from "../utils/Logger";
import { ProtoConfig } from "../socketserver/protocol/ProtoConfig";
import { ProtoUtils } from "../socketserver/protocol/ProtoUtils";
import { WebSocketClient } from "./WebSocketClient";
import { RetCodeDefine } from "../../config/RetCodeDefine";
import { SocketMsg } from "../socketserver/socket/SocketMsg";

export class SocketClientReqManager {

    public static instance: SocketClientReqManager = new SocketClientReqManager();

    public onSocketMsg(socketMsg: SocketMsg, client: WebSocketClient) {
        Logger.log("SocketClientReqManager.onSocketMsg==", socketMsg);
        if (socketMsg.protoName == ProtoConfig.resp_error_code) {
            this.onErrorCode(socketMsg, client)
        }
    }

    private onErrorCode(socketMsg: SocketMsg, client: WebSocketClient) {
        let respPack = ProtoUtils.DeserializeBinary(ProtoConfig.resp_error_code, socketMsg.protoData);
        let retCode: number = respPack.getRetcode();
        let msg: string = RetCodeDefine.code2Msg.get(retCode);
        Logger.warn("onErrorCode==", client.id, retCode, msg)
    }

    private createSysPack() {
        let sysPack = ProtoUtils.Create(ProtoConfig.struct_user_sys_info);
        sysPack.setOs("cc.sys.os");
        sysPack.setPlatform(1);
        sysPack.setOsmainversion("cc.sys.osMainVersion");
        sysPack.setOsversion("cc.sys.osVersion");
        sysPack.setGameversion("VersionManager.instance.nowVersion");
        return sysPack;
    }

    public req_login(userName: string, pass: string, client: WebSocketClient) {
        let protoPack = ProtoUtils.Create(ProtoConfig.req_login);
        protoPack.setUsername(userName);
        protoPack.setPass(pass);
        let sysPack = this.createSysPack();
        protoPack.setSysinfo(sysPack);
        Logger.log("login==", protoPack);
        client.sendProto(protoPack, (socketMsg: SocketMsg) => {
            let respPack = ProtoUtils.DeserializeBinary(ProtoConfig.resp_login, socketMsg.protoData);
            Logger.log("login resp=", socketMsg, respPack)
            let retCode: number = respPack.getRetcode();
            if (retCode == RetCodeDefine.OK) {
                Logger.log("登录成功", client)
            } else {
                let msg: string = RetCodeDefine.code2Msg.get(retCode);
                Logger.warn("登录失败==", retCode, msg)
            }
        });
    }

    //注册
    public req_register(userName: string, pass: string, client: WebSocketClient) {
        let protoPack = ProtoUtils.Create(ProtoConfig.req_register);
        protoPack.setUsername(userName);
        protoPack.setPass(pass);
        Logger.log("req_register==", protoPack);
        client.sendProto(protoPack, (socketMsg: SocketMsg) => {
            let respPack = ProtoUtils.DeserializeBinary(ProtoConfig.resp_register, socketMsg.protoData);
            Logger.log("req_register resp=", socketMsg, respPack)
            let retCode: number = respPack.getRetcode();
            if (retCode == RetCodeDefine.OK) {
                Logger.log("注册成功", client)
            } else {
                let msg: string = RetCodeDefine.code2Msg.get(retCode);
                Logger.warn("注册失败==",client, retCode, msg)
            }
        });
    }

}